<template>
	<view class="content">
		<view class="navbar">
			<view 
				v-for="(item, index) in navList" :key="index" 
				class="nav-item" 
				:class="{current: tabCurrentIndex === index}"
				@click="tabClick(index)"
			>
				{{item.text}}
			</view>
		</view>
		<swiper :current="tabCurrentIndex" class="swiper-box" duration="300" @change="changeTab">
			<swiper-item class="tab-content" v-for="(tabItem,tabIndex) in navList" :key="tabIndex">
				<scroll-view 
					class="list-scroll-content" 
					scroll-y
					@scrolltolower="reachBottom"
				>
				<empty v-if="tabItem.orderList.length === 0"></empty>
				<view class="orderlist">
					<view class="uni-list">
						<view class="uni-list-cell bor10" hover-class="uni-list-cell-hover" v-for="(item, index) in tabItem.orderList"  :key="index">
							<view class="on">订单编号：{{item.order_id || ''}}</view>
							<view class="uni-media-list">
								<view class="uni-media-list-logo">
									<image :src="item.nav_list_logo" mode="aspectFill"></image>
								</view>
								<view class="uni-media-list-body">
									<view class="uni-media-list-text-top uni-ellipsis">{{item.title}}</view>
									<view class="uni-media-list-text-bottom "><text v-text="cutText(item)"></text></view>
									<view class="price"><text>{{item.nav_ori_price || '0.00'}}</text>{{item.money || '0.00'}}</view>
								</view>
							</view>
							<view class="state">
								<text class="time">{{item.update_time || ''}}</text>
								<text v-if="item.status == 0" class="btn" @click="navToDetailPage(item,'/pages/order/pay')">
									去支付
								</text>
								<text v-if="item.state == 1" class="btn1">
									已完成
								</text>
							</view>
						</view>
					</view>
				</view>
				<uni-load-more :status="tabItem.loadingType"></uni-load-more>
				</scroll-view>
			</swiper-item>
		</swiper>
	</view>
</template>
<script>
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/empty";
	import Json from '@/Json';
	export default {
		components: {
			uniLoadMore,
			empty
		},
		data() {
			return {
				tabCurrentIndex: 0,
				navList: [{
						state: 0,
						text: '全部订单',
						loadingType: 'more',
						orderList: [],
						page: 1,
					},
					{
						state: 1,
						text: '未支付订单',
						loadingType: 'more',
						orderList: [],
						page: 1,
					},
					{
						state: 2,
						text: '已完成订单',
						loadingType: 'more',
						orderList: [],
						page: 1,
					}
				],
			};
		},
		
		onLoad(options){
			/**
			 * 修复app端点击除全部订单外的按钮进入时不加载数据的问题
			 * 替换onLoad下代码即可
			 */
			this.tabCurrentIndex = +options.state;
			// #ifndef MP
			this.loadData()
			// #endif
			// #ifdef MP
			if(options.state == 0){
				this.loadData()
			}
			// #endif
			
		},
		
		 
		methods: {
			reachBottom(){
				let index = this.tabCurrentIndex,
					navItem = this.navList[index],
					state = navItem.state;
				
				if(navItem.loadingType === 'loading'){
					return false;
				}
				if(navItem.loadingType === 'noMore'){
					return false;
				}
				navItem.page++;
				console.log('加载更多',navItem.page)
				this.loadData('add');
				
			},
			
			cutText(item){
				if(item && item.nav_list_title){
					return item.nav_list_title.substring(0,25);
				}else{
					return '';
				}
			},
			
			//获取订单列表
			async loadData(source){
				//这里是将订单挂载到tab列表下
				let index = this.tabCurrentIndex,
					navItem = this.navList[index],
					state = navItem.state,
					params = {};
				
				if(source === 'tabChange' && navItem.loaded === true){
					//tab切换只有第一次需要加载数据
					return;
				}
				if(navItem.loadingType === 'loading'){
					//防止重复加载
					return;
				}
				
				navItem.loadingType = 'loading';
				params.status = state;
				params.page = navItem.page ;
				params.pagesize = 3 ;
				
				await this.$http.post('api/order/userorder',params).then(res=>{
					if(res.code === this.sucCode){
						let orderList = res.data.list || [];
						orderList.forEach(item=>{
							navItem.orderList.push(item);
						})
						//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
						this.$set(navItem, 'loaded', true);
						
						//判断是否还有数据， 有改为 more， 没有改为noMore 
						navItem.loadingType = orderList.length < params.pagesize  ? 'noMore' : 'more';
						
					}else{
						this.$mHelper.toast(res.msg);
					}
					
				}).catch(err=>{
					console.log(err);
				})
				
				/* setTimeout(()=>{
					let orderList = Json.orderList.filter(item=>{
						//演示数据所以自己进行状态筛选
						if(state === 0){
							//0为全部订单
							return item;
						}
						return item.state === state
					});
					orderList.forEach(item=>{
						navItem.orderList.push(item);
					})
					//loaded新字段用于表示数据加载完毕，如果为空可以显示空白页
					this.$set(navItem, 'loaded', true);
					
					//判断是否还有数据， 有改为 more， 没有改为noMore 
					navItem.loadingType = 'more';
				}, 600); */	
			}, 
	
			//swiper 切换
			changeTab(e){
				this.tabCurrentIndex = e.target.current;
				this.loadData('tabChange');
			},
			//顶部tab点击
			tabClick(index){
				this.tabCurrentIndex = index;
			},
			
			navToDetailPage(item,url) {
				
				this.$mHelper.navTo('/pages/order/pay','push',{id:item.id,money:item.money});
				/* uni.navigateTo({
					url: url
				}) */
			},
	
		},
	}
</script>
<style lang="scss">
	page, .content{
		background: #fff;
		height: 100%;
	}
	
	.swiper-box{
		height: calc(100% - 40px); margin-top: 0;
	}
	.list-scroll-content{
		height: 100%;
	}
	
	.navbar{
		display: flex;
		height: 40px;
		background: #fff;
		box-shadow: 0 1px 5px rgba(0,0,0,.06);
		position: relative;
		z-index: 10;
		.nav-item{
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 15px;
			color: #000;
			position: relative;
			&.current{
				color: #dc1011;
				&:after{
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 120upx;
					height: 0;
					border-bottom: 2px solid #dc1011;
				}
			}
		}
	}
.orderlist .uni-list{ padding-bottom: 20upx}
.orderlist .uni-media-list-logo {width: 320upx;height: 200upx;}
.orderlist .uni-media-list-logo image{border-radius: 15upx;}
.orderlist .uni-media-list-body {justify-content: inherit;height: 200upx; position: relative;}
.orderlist .uni-media-list-text-top{margin-bottom:15upx;font-size: 30upx; }
.orderlist .uni-media-list-text-bottom {font-size: 24upx;line-height: 36upx;}
.orderlist .uni-list::before,.orderlist .uni-list-cell::after,.orderlist .uni-list:after{height: 0}
.orderlist .price{ position: absolute;right:0;bottom:0;color: #ff0000;font-size: 30upx;}
.orderlist .price text{ color:#999; text-decoration:line-through ;font-size: 24upx;padding-right: 10upx;}
.orderlist .uni-list-cell{ clear:both;display: block;}
.orderlist .on{clear:both;display: block;border-bottom:#eee solid 1px; border-top:#eee solid 1px; padding:10upx 30upx}
.orderlist .state{clear:both;display: block;border-bottom:#eee solid 1px; border-top:#eee solid 1px; padding:10upx 30upx;overflow: hidden;}
.orderlist .state .time{ float:left}
.orderlist .state .btn{color: #fff;background: #dc1011;border-radius: 40upx;padding: 2upx 20upx;float:right}
.orderlist .state .btn1{color: #666;background: #ddd;border-radius: 40upx;padding: 2upx 20upx;float:right}
</style>	
